﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace UtilitiesMvcUI.Infrastructure
{
    public class CultureRouteHandler : MvcRouteHandler
    {
        protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            foreach (var paramUrl in requestContext.RouteData.Values)
            {
                requestContext.HttpContext.Items[paramUrl.Key] = paramUrl.Value;
            }
            var ci = new CultureInfo("en");
            try
            {
                var cultureString = requestContext.RouteData.Values["culture"].ToString();
                ci = new CultureInfo(cultureString);
            }
            catch
            {

            }
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
            return base.GetHttpHandler(requestContext);
        }
    }
}